package com.dingding.gmall.member.controller;

import com.dingding.gmall.common.exception.BizCodeEnum;
import com.dingding.gmall.common.utils.PageUtils;
import com.dingding.gmall.common.utils.R;
import com.dingding.gmall.member.dto.MemberLoginDTO;
import com.dingding.gmall.member.dto.MemberRegistDTO;
import com.dingding.gmall.member.entity.MemberEntity;
import com.dingding.gmall.member.exception.PhoneException;
import com.dingding.gmall.member.exception.UserNameException;
import com.dingding.gmall.member.feign.CouponFeignService;
import com.dingding.gmall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 会员
 *
 * @author liudingding
 * @email sunlightcs@gmail.com
 * @date 2020-07-02 22:38:49
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    CouponFeignService couponFeignService;


    @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R membercoupons = couponFeignService.membercoupons();
        return R.ok().put("member",memberEntity)
                .put("coupons",membercoupons.get("coupon"));
    }


    @PostMapping("login")
    public R login(@RequestBody MemberLoginDTO loginDTO){
        MemberEntity entity = memberService.login(loginDTO);
        if (entity == null) {
            return R.error(BizCodeEnum.ACCOUNT_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.ACCOUNT_PASSWORD_EXCEPTION.getMsg());
        }
        return R.ok().setData(entity);
    }

    @PostMapping("regist")
    public R regist(@RequestBody  MemberRegistDTO memberRegistDTO){
        try {
            memberService.regist(memberRegistDTO);
        } catch (PhoneException e) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UserNameException e) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
